package com.qingpeng.pay.fragment;

import android.content.Intent;
import android.os.Build;
import android.text.Html;
import android.widget.TextView;

import com.qingpeng.pay.R;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseFragment;
import com.qingpeng.pay.beans.LoginBean;
import com.qingpeng.pay.beans.MoneyBean;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.service.NLService;
import com.qingpeng.pay.service.NotificationCollectorMonitorService;
import com.qingpeng.pay.utils.APP;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainFragment extends BaseFragment {


    @BindView(R.id.title)
    TitleBar title;
    @BindView(R.id.user_money)
    TextView userMoney;
    @BindView(R.id.off_money)
    TextView offMoney;
    @BindView(R.id.no_money)
    TextView noMoney;
    @BindView(R.id.pay)
    TextView pay;
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.number1)
    TextView number1;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    public void onResume() {
        super.onResume();
        showData();
    }

    private void showData() {
        Call<LoginBean> userinfo = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).userinfo();
        userinfo.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                LoginBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(mContext, body.getInfo());
                    return;
                }
                APP.userInfo = body.getData();
                if (APP.userInfo.getTake_status().equals("0")) {
                    pay.setText("接单");
                    APP.is=false;
                } else {
                    APP.is=true;
                    pay.setText("停止接单");
                }
            }
            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                ToastUtils.showToast(mContext, "网络请求错误,请稍后重试");

            }
        });
        Call<MoneyBean> getaccount = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).getaccount();
        getaccount.enqueue(new Callback<MoneyBean>() {
            @Override
            public void onResponse(Call<MoneyBean> call, Response<MoneyBean> response) {
                MoneyBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(getActivity(), body.getInfo());
                    return;
                }
                Double s = (body.getData().getTolscore()*100) - 100000;
                // "<font color='#FF0000'>" + str4 + "</font>"
                userMoney.setText(Html.fromHtml("账户余额:   " + "<font color='#ff7115'>" + body.getData().getTolscore() + "</font>元"));
                if (body.getData().getTolscore() > 1000) {
                    offMoney.setText(Html.fromHtml("不可接单金额:   " + "<font color='#ff7115'>" + 1000 + "</font>元"));
                } else {
                    offMoney.setText(Html.fromHtml("不可接单金额:   " + "<font color='#ff7115'>" + body.getData().getTolscore() + "</font>元"));
                }
                if (s > 0) {
                    noMoney.setText(Html.fromHtml("可接单金额:   " + "<font color='#ff7115'>" + String.format("%.2f", s/100)+ "</font>元"));
                } else {
                    noMoney.setText(Html.fromHtml("可接单金额:   " + "<font color='#ff7115'>" + 0 + "</font>元"));
                }
                number.setText(body.getData().getWxQRnum() + "个");
                number1.setText(body.getData().getZfbQRnum() + "个");
            }

            @Override
            public void onFailure(Call<MoneyBean> call, Throwable t) {
                ToastUtils.showToast(mContext, "网络请求错误,请稍后重试");
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @OnClick(R.id.pay)
    public void onViewClicked() {
        Call<LoginBean> userinfo = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).userinfo();
        userinfo.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                LoginBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(mContext, body.getInfo());
                    return;
                }
                APP.userInfo = body.getData();
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                ToastUtils.showToast(mContext, "网络请求错误,请稍后重试");
            }
        });
        pay.setClickable(false);
        Call<NullBean> start = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).start();
        start.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(mContext, body.getInfo());
                    pay.setClickable(true);
                    return;
                }
                if (APP.userInfo.getTake_status().equals("0")) {
                    APP.is=true;
                    pay.setText("停止接单");
                    if (Build.VERSION.SDK_INT >= 26) {
                        mContext.startForegroundService(new Intent(getActivity(), NotificationCollectorMonitorService.class));
                        mContext.startForegroundService(new Intent(getActivity(), NLService.class));
                    } else {
                        mContext.startService(new Intent(getActivity(), NotificationCollectorMonitorService.class));
                        mContext.startService(new Intent(getActivity(), NLService.class));
                    }
                } else {
                    pay.setText("接单");
                    APP.is=false;
                    mContext.stopService(new Intent(getActivity(), NotificationCollectorMonitorService.class));
                    mContext.stopService(new Intent(getActivity(), NLService.class));
                }
                ToastUtils.showToast(mContext, body.getInfo());
                pay.setClickable(true);
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                pay.setClickable(true);
                ToastUtils.showToast(mContext, "网络请求错误,请稍后重试");
            }
        });
    }
}
